#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;

class Solution1 {
    public:
        int firstMissingPositive(int A[], int n) {
            if (n == 0) return 1;
            for (int i = 0; i < n; i++) {
                while (A[i] > 0 && A[i] <= n && A[i] != i+1 && A[A[i]-1] != A[i]) {
                    int tmp = A[A[i]-1];
                    A[A[i]-1] = A[i];
                    A[i] = tmp;
                }
            }
            for (int i = 0; i < n; i++)
               if (A[i] != i+1) return i+1;
            return n+1;
        }
};

class Solution {
public:
    int firstMissingPositive(int A[], int n) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        if (n == 0) return 1;
        if (n == 1 && A[0] != 1) return 1;
        for (int i = 0; i < n; i++) {
             while(A[i] != i && A[i] >= 0 && A[i] < n) {
                if (A[A[i]] == A[i]) break;
                int tmp = A[A[i]];
                A[A[i]] = A[i];
                A[i] = tmp;    
            }   
        }
        for (int i = 1; i < n; i++)
            if (A[i] != i) return i;
        return n;
    }
};



//test in 1337 online judge and pass all the test
int main(int argc, char **argv)
{
    Solution mySolution;

    cout << "-----------------Test 1--------------------" << endl;
    int a[] = {3,4,-1,1};
    int result = mySolution.firstMissingPositive(a, sizeof(a)/sizeof(int));
    cout << result << endl;


    cout << "-----------------Test 2--------------------" << endl;


    cout << "-----------------Test 3--------------------" << endl;


    cout << "-----------------Test 4--------------------" << endl;


    cout << "-----------------Test 5--------------------" << endl;



}
